package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Robot;
import io.xxx.xbutler.sdk.TanJingClient;
import io.xxx.xbutler.sdk.request.RobotPullGroupAdoptRequest;
import io.xxx.xbutler.sdk.response.RobotPullGroupAdoptResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 【直接回调】机器人收到入群邀请回调（兼容PC）<br/>
 * 机器人收到入群邀请回调码：4506。
 * 收到此回调的条件：被邀请的群群成员人数超过40人；
 * 机器人开通了自动通过群邀请时正常通过不会回调4506，当机器人入群被风控时，无法自动通过群聊邀请，会将4506回调给到商家；
 * 机器人未开通自动通过群邀请时会将4506回调给商家；
 * <pre>
 * {
 *     "vcMerchantNo": "202004240382704",
 *     "vcRobotWxId": "srw8807",
 *     "vcRobotSerialNo": "48D2120A81A472F461D1B640AC9243C0",
 *     "vcSerialNo": "20220831145133442310121875037",
 *     "nType": 4506,
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "nTimeStamp": 1661928693897,
 *     "Data": {
 *         "vcChatRoomName": "凯撒西部",
 *         "vcChatRoomBase64Name": "5Yev5pKS6KW/",
 *         "vcFriendWxId": "",
 *         "vcFriendWxIdSerialNo": "",
 *         "vcFullUrl": "",
 *         "vcHeadImgUrl": "http://wx.qlogo.cn/mmcrhead/GibrLBvzd7U2FnbXic2CMKT6MMEIGrWfsDCdpxe88HnaWFD8p7iaOibeKqxNrDXjdia6HMbvpfnEkicHgYZqdpGbzodlsu5tStdDUL/0",
 *         "MsgDesc": "",
 *         "IsEnterpriseChatroom": false
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4506Handler implements CallbackHandler {

    @Resource
    private RobotMapper robotMapper;

    @Resource
    private TanJingClient client;

    @Override
    public String name() {
        return "机器人收到入群邀请回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4506;
    }

    @Override
    public void handle(Callback callback) {
        String serialNo = callback.getSerialNo();
        String robotId = callback.getRobotId();
        String robotSerialNo = callback.getRobotSerialNo();
        JSONObject data = (JSONObject) callback.getData();
        String roomName = data.getString("vcChatRoomName");

        Robot robot = robotMapper.selectById(robotId);
        if (robot == null) {
            log.warn("机器人信息不存在[robotId:{}]", robotId);
            return;
        }

        if (robot.getAutoJoinRoom()) {
            RobotPullGroupAdoptRequest request = new RobotPullGroupAdoptRequest()
                    .setRobotSerialNo(robotSerialNo)
                    .setSerialNo(serialNo);
            RobotPullGroupAdoptResponse response = client.execute(request);
            if (response.isSuccess()) {
                log.info("好友邀请入群请求已通过[robotId:{}, robotSerialNo:{}, serialNo:{}, roomName:{}]",
                        robotId, robotSerialNo, serialNo, roomName);
            } else {
                log.error("好友邀请入群请求通过失败[robotId:{}, robotSerialNo:{}, serialNo:{}, roomName:{}, request:{}, response:{}]",
                        robotId, robotSerialNo, serialNo, roomName, JSON.toJSONString(request.params()), JSON.toJSONString(response));
            }
        }
    }
}
